package com.example.base.security.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.example.base.config.ConfigService;
import com.example.base.security.domain.SysUserEntity;
import com.example.base.security.service.PasswordService;
import com.example.base.security.service.SysUserService;
import com.example.base.utils.Md5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PasswordServiceImpl implements PasswordService {

    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private ConfigService configService;

    @Override
    public void restartPassword(List<String> userId) {
        String password=configService.getSysConfigByKey("restartPassword");
        password=this.strToPassword(password);
        LambdaUpdateWrapper<SysUserEntity> lambdaUpdateWrapper=new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.set(SysUserEntity::getPassword,password);
        lambdaUpdateWrapper.in(SysUserEntity::getUserId,userId);
        sysUserService.update(lambdaUpdateWrapper);
    }

    @Override
    public String strToPassword(String input) {
        String md5=Md5Utils.encrypt(input);
        return bCryptPasswordEncoder.encode(md5);
    }

    @Override
    public String md5ToPassword(String input) {
        return bCryptPasswordEncoder.encode(input);
    }
}
